/*  
 *  ------   [ZB_07a] - Use of Expansion board     -------- 
 *  
 *  Explanation: This example shows how to use two XBee modules
 *  simultaneously in Waspmote using the Expansion board. Two 
 *  different objects are created in this code, each one is used 
 *  for each module. One is connected in the standard socket (SOCKET0)
 *  and the other one is connected to the Expansion board (SOCKET1)
 *  
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */

#include <WaspXBeeZB.h>

// define objects
WaspXBeeZB xbee0 = WaspXBeeZB();
WaspXBeeZB xbee1 = WaspXBeeZB();

// pointer to an XBee packet structure 
packetXBee* packet0;  
packetXBee* packet1; 

// create data fields for each packet
char* data0 = "SOCKET0 message\r\n";
char* data1 = "SOCKET1 message\r\n";



void setup()
{ 
  // init USB port
  USB.ON();
  USB.println(F("ZB_07a example"));   

  //////////////////////////
  // 1. init XBees
  //////////////////////////

  // 1.1. initialize object in SOCKET0
  xbee0.ON(SOCKET0);

  // 1.2. initiliaze object in SOCKET1
  xbee1.ON(SOCKET1);    
  
  delay(10000);


  //////////////////////////
  // 2. check both XBee's network parameters
  //////////////////////////
  checkNetworkParams();
}



void loop()
{ 
  //////////////////////////////////////
  // 3. send packet via SOCKET0 
  //////////////////////////////////////

  // 3.1. set params to send:
  packet0=(packetXBee*) calloc(1,sizeof(packetXBee)); // Memory allocation
  packet0->mode=UNICAST; // Choose transmission mode: UNICAST or BROADCAST

    // 3.2. set destination XBee parameters
  xbee0.setDestinationParams(packet0, "0013A2004076915F", data0); 

  // 3.3. send XBee packet
  xbee0.sendXBee(packet0);

  // 3.4. check TX flag
  USB.print(F("xbee0 sent packet: ")); 

  if( xbee0.error_TX == 0 ) 
  {
    USB.println(F("ok"));
  }
  else 
  {
    USB.println(F("error"));  
  }

  // 3.5. free variables
  free(packet0);
  packet0=NULL;

  delay(1000);



  //////////////////////////////////////
  // 4. send packet via SOCKET1
  //////////////////////////////////////

  // 4.1. set params to send:
  packet1=(packetXBee*) calloc(1,sizeof(packetXBee)); // Memory allocation
  packet1->mode=UNICAST; // Choose transmission mode: UNICAST or BROADCAST

    // 4.2. set destination XBee parameters
  xbee1.setDestinationParams(packet1, "0013A2004076915F", data1); 

  // 4.3. send XBee packet
  xbee1.sendXBee(packet1);

  // 4.4 check TX flag  
  USB.print(F("xbee1 sent packet: "));   

  if( xbee1.error_TX == 0 ) 
  {
    USB.println(F("ok"));
  }
  else 
  {
    USB.println(F("error"));  
  }

  // 4.5. free variables
  free(packet1);
  packet1=NULL;



  // wait for five seconds
  delay(5000);
}






/*******************************************
 *
 *  checkNetworkParams - Check operating
 *  network parameters in the XBee module
 *
 *******************************************/
void checkNetworkParams()
{
  // 1. get operating 64-b PAN ID
  xbee0.getOperating64PAN();
  xbee1.getOperating64PAN();

  // 2. wait for association indication
  xbee0.getAssociationIndication();
  xbee1.getAssociationIndication();

  USB.println(F("Wait for association"));
  while( (xbee0.associationIndication != 0) &&
         (xbee1.associationIndication != 0)   )
  { 
    delay(2000);

    USB.print(F("."));   

    xbee0.getAssociationIndication();
    xbee1.getAssociationIndication();
  }

  USB.println(F("\nBoth modules joined a network!"));

}




